package com.warm.pump.module.work.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import com.warm.pump.module.work.dao.gen.WorkDbDao;
import com.warm.pump.module.work.bean.po.gen.WorkDb;
import com.warm.pump.module.work.service.IWorkDbService;
import com.warm.pump.frame.gen.bean.PageBean;


@Service("WorkDbService")
public class WorkDbServiceImpl implements IWorkDbService {
    @Autowired
    private WorkDbDao workDbDao;

    @Override
    public WorkDb queryObject(Long id) {
        return workDbDao.selectByPrimaryKey(id);
    }

    public WorkDb queryObject(Map<String, Object> map) {
        return workDbDao.selectByMap(map);
    }

    @Override
    public List<WorkDb> queryList(Map<String, Object> map) {
        return workDbDao.getListByMap(map);
    }

    @Override
    public PageBean getPageBeanByMap(Map map, int pageNo, int pageSize) {
        return workDbDao.getPageBeanByMap(map, pageNo, pageSize);
    }


    @Override
    public int queryTotal(Map<String, Object> map) {
        return workDbDao.countTotalByMap(map);
    }

    @Override
    public void save(WorkDb workDb) {
        workDbDao.insertSelective(workDb);
    }

    @Override
    public void update(WorkDb workDb) {
        workDbDao.updateByPrimaryKeySelective(workDb);
    }

    @Override
    public void delete(Long id) {
        workDbDao.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(Map<String, Object> map) {
        workDbDao.deleteByMap(map);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("id_in", ids);
        workDbDao.deleteByMap(map);
    }

    public boolean testConnection(String url, String user, String pass) {
        Connection conn = null;
        try {
            DriverManager.setLoginTimeout(2);
            conn = DriverManager.getConnection(url, user, pass);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            this.releaseConnection(conn);
        }
    }

    private void releaseConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
            }
        }
    }

}
